package club.caihappy.service.impl;

import club.caihappy.constants.SystemConstants;
import club.caihappy.domain.ResponseResult;
import club.caihappy.domain.entity.Article;
import club.caihappy.domain.entity.Category;
import club.caihappy.domain.vo.CategoryVO;
import club.caihappy.mapper.CategoryMapper;
import club.caihappy.service.ArticleService;
import club.caihappy.service.CategoryService;
import club.caihappy.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2022-10-26 16:47:54
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    @Override
    public ResponseResult getCategoryList() {
        //查询文章表已发布文章
        List<Article> list = articleService.list(new LambdaQueryWrapper<>(Article.class).eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL));
        //获取文章表id并去重
        List<Long> categoryIds = list.stream().map(a -> a.getCategoryId()).distinct().collect(Collectors.toList());
        //根据id查询分类表
        List<Category> categories = listByIds(categoryIds);
        //过滤掉状态不正常的
        List<Category> res = categories.stream().filter(c -> SystemConstants.CATEGORY_STATUS_NORMAL.equals(c.getStatus())).collect(Collectors.toList());
        //封装为vo
        List<CategoryVO> categoryVOS = BeanCopyUtils.copyBeanList(res, CategoryVO.class);
        return ResponseResult.okResult(categoryVOS);


    }
}




















